package com.hxgz.steel.controller.portal;

import com.hxgz.steel.common.Const;
import com.hxgz.steel.common.ResponseCode;
import com.hxgz.steel.common.ServerResponse;
import com.hxgz.steel.dao.MerchantMapper;
import com.hxgz.steel.domain.AskBuy;
import com.hxgz.steel.domain.AskInfo;
import com.hxgz.steel.domain.Merchant;
import com.hxgz.steel.domain.User;
import com.hxgz.steel.dto.CategoryDto;
import com.hxgz.steel.dto.TableDto;
import com.hxgz.steel.service.AskBuyService;
import com.hxgz.steel.service.GoodsManageService;
import com.hxgz.steel.util.UploadPic;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@RestController
@RequestMapping("askBuy")
@Api(tags = "AskBuyController", description = "前台-求购相关")
public class AskBuyController {

  @Autowired
  private AskBuyService askBuyService;
  @Autowired
  private GoodsManageService goodsManageService;
@Autowired
private MerchantMapper merchantMapper;
  /**
   * 用户发起求购
   */
  @RequestMapping(value = "initiateAskBuy/{remark}/{time}", method = RequestMethod.POST)
  @ApiOperation("实单求购")
  public ServerResponse initiateAskBuy(HttpServletRequest httpServletRequest,
      @ApiParam("备注") @PathVariable(required = false) String remark,
      @ApiParam("报价时限") @PathVariable(required = false) Integer time,
                                   @RequestBody  List<AskInfo> table) {
    User user = Const.checkUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    if (!(new Integer(3).equals(user.getStatus()))){
      return ServerResponse.createByErrorMessage("请进入个人中心的基础资料进行实名认证");
    }
    if (remark.length()>100){
      return ServerResponse.createByErrorMessage("备注字段不得超过100");
    }
    AskBuy askBuy = new AskBuy();
    askBuy.setUserId(user.getId());
    askBuy.setTimeLimit(time);
    askBuy.setRemark(remark);
    askBuy.setStatus(0);
    return askBuyService.initiateAskBuy(askBuy, table,user);
  }

  @RequestMapping(value = "initiateAskBuy/{remark}/{time}/{merchantName}/{merchantId}", method = RequestMethod.POST)
  @ApiOperation("实单求购")
  public ServerResponse initiateAskBuy(HttpServletRequest httpServletRequest,
                                       @ApiParam("商家名称") @PathVariable(required = false) String merchantName,
                                       @ApiParam("商家id") @PathVariable(required = false) Long merchantId,
                                       @ApiParam("备注") @PathVariable(required = false) String remark,
                                       @ApiParam("报价时限") @PathVariable(required = false) Integer time,
                                       @RequestBody  TableDto table) {
    /**
     * 数据格式校验
     */
    List<AskInfo> tempList = table.getTable();
    for (AskInfo askInfo : tempList) {
      if(!askInfo.getQuantity().matches("^[1-9]\\d*$")){
        return ServerResponse.createByErrorMessage("数量请填正整数!");
      }
      if(StringUtils.isEmpty(askInfo.getCommodity())){
        return ServerResponse.createByErrorMessage("品名不能为空!");
      }
    }
    User user = Const.checkUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
              ResponseCode.NEED_LOGIN.getDesc());
    }
    if (!(new Integer(3).equals(user.getStatus()))){
      return ServerResponse.createByErrorMessage("请进入个人中心的基础资料进行实名认证");
    }
    AskBuy askBuy = new AskBuy();
    askBuy.setUserId(user.getId());
    askBuy.setTimeLimit(time);
    askBuy.setAskFileUrl(table.getFileUrl());
    if(merchantId!=0){
      //实单求购 不指定的时候 merchaId传的0
      askBuy.setMerchantId(merchantId);
      askBuy.setMerchantName(merchantName);
      Merchant merchant = merchantMapper.selectByPrimaryKey(merchantId);
      if(merchant.getUserId().equals(user.getId())){
        return ServerResponse.createByErrorMessage("不能对自己的店铺发起实单求购");
      }
    }
    askBuy.setRemark(remark);
    return askBuyService.initiateAskBuy(askBuy, table.getTable(),user);
  }

  @ApiOperation("前台-求购-下载模板")
  @RequestMapping(value = "outCataLogExcle.do", method = RequestMethod.GET)
  public void outCataLogExcle(HttpServletResponse response) throws Exception {
    askBuyService.outCataLogExcle(response);
  }

  @ApiOperation("前台-求购-求购上传")
  @PostMapping(value = "/uploadExcle.do", headers = "content-type=multipart/form-data")
  public ServerResponse<List<AskInfo>> uploadExcle(
      @RequestParam(value = "file", required = true) MultipartFile file, HttpServletRequest request){
    //上传文件到服务器
    UploadPic uploadPic = new UploadPic();
    String filePath = uploadPic.upload(file);
    try {
      return askBuyService.uploadExcel(filePath);
    } catch (Exception e) {
      e.printStackTrace();
      return ServerResponse.createByErrorMessage("模板文件不匹配,请上传模板文件，请检查模板文件中数量是否带有小数,格式是否正确等");
    }
  }

  @ApiOperation(value = "求购各种下拉",notes = "name  品名，商品名称，材质，特性 id 1-风类  2-水类  3-电类  4-消防类")
  @RequestMapping(value = "getTypeSelect.do",method = RequestMethod.POST)
  public ServerResponse<List<CategoryDto>> getTypeSelect(String name, Integer id){
    List<CategoryDto> typeList = goodsManageService.getDown(name,id);
    return ServerResponse.createBySuccess(typeList);
  }


}
